package com.hrbxlh.virtualization.result;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.hrbxlh.virtualization.enums.ResponseEnum;


import java.io.Serializable;

/**
 * 响应结果封装
 *
 * @author cheer
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RestResponse<T> implements Serializable {

    private int code;
    private String message;
    private T data;

    public RestResponse() {
    }

    public RestResponse(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private RestResponse(ResponseEnum result) {
        this.code = result.getCode();
        this.message = result.getMessage();
    }

    private RestResponse(ResponseEnum result, T data) {
        this.code = result.getCode();
        this.message = result.getMessage();
        this.data = data;
    }

    private RestResponse(ResponseEnum result, String message) {
        this.code = result.getCode();
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public static <T> RestResponse<T> ok() {
        return new RestResponse<>(ResponseEnum.OK);
    }

    public static <T> RestResponse<T> ok(T data) {
        return new RestResponse<>(ResponseEnum.OK, data);
    }

    public static <T> RestResponse<T> ok(String message, T data) {
        return new RestResponse<>(ResponseEnum.OK.getCode(), message, data);
    }

    public static <T> RestResponse<T> okWithMessage(String message) {
        return new RestResponse<>(ResponseEnum.OK, message);
    }

    public static <T> RestResponse<T> error(ResponseEnum result) {
        return new RestResponse<>(result);
    }

    public static <T> RestResponse<T> error(ResponseEnum result, String message) {
        return new RestResponse<>(result, message);
    }

    public static <T> RestResponse<T> errorWithMessage(String message) {
        return new RestResponse<>(ResponseEnum.ERROR, message);
    }

    @JsonIgnore
    public boolean isOk() {
        return ResponseEnum.OK.getCode().equals(code);
    }
}
